package com.globalwave.product.web;


import com.globalwave.base.BaseAction;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.product.entity.Product;
import com.globalwave.product.entity.ProductCriterion;
import com.globalwave.product.service.ProductService;
import com.globalwave.system.web.annotations.Pid;
import com.opensymphony.xwork2.Preparable;

public class ProductAction extends BaseAction implements Preparable {
   
    private static final long serialVersionUID = 1L;

    private ProductService productService = null ;
    private ProductCriterion criterion = null ;
    private Product product ;
    
    public String execute() throws Exception {
        return this.list();    
        
    }
    
    public String list() throws Exception {        
        ArrayOrderList order = new ArrayOrderList() ;
        order.addAsc("product_id") ;
        ArrayPageList<Product> pageList = productService.query(criterion,pageIndex,pageSize,order) ;

        renderXML(pageList.asXML()) ;
        
        return null ;    
        
    }
    
    @Pid(value=132)
    public String create()  throws Exception {        
                
        final Object newProduct = productService.create(product) ;
        
        renderXML(this.getSuccessCreateMessage(newProduct).asXML()) ;
        
        return null;    
        
    }
    
    @Pid(value=133)
    public String update()  throws Exception {     
        
        productService.update(product) ;

        renderXML(this.getSuccessUpdateMessage(product).asXML()) ;
        
        return null;    
        
    }
    
    @Pid(value=134)
    public String delete()  throws Exception {
        
        if (ids == null) {
            productService.delete(product) ;
        } else {
            productService.deleteAll(ids) ;
        }

        renderXML(this.getSuccessDeleteMessage().asXML()) ;
        
        return null;    
        
    }
    
    
    public void prepare() throws Exception {
        // TODO Auto-generated method stub

    }
        
    // ------------------------------------------------------------------------
    
    // ioc
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    // entity...
    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

	public ProductCriterion getCriterion() {
		return criterion;
	}

	public void setCriterion(ProductCriterion criterion) {
		this.criterion = criterion;
	}
}
